/**
 * Description: 服务端 使用IO多路复用实现并发
 * File: server_recv.c |Time: 2021-04-10 08:36
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/select.h>

#define ERR(msg) do {            \
             perror(msg);        \
             printf("%s:%s:%d\n", __FILE__, __func__, __LINE__); \
             exit(EXIT_FAILURE); \
         }while(0)

#define BUF_SIZE 100

int main(int argc, char *argv[])
{
    if (argc < 3) {
        printf("Usage: %s <ip> <port>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    // 使用socket创建文件描述
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd == -1) {
        ERR("socket");
    }

    // 设置端口复用(端口复用要在bind之前设置)
    int reuse_port = 1;
    if (-1 == setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &reuse_port, sizeof(reuse_port))) 
    {
        ERR("setsockopt");
    }


    // 绑定socket参数
    struct sockaddr_in sockaddr;

    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(atoi(argv[2]));
    sockaddr.sin_addr.s_addr = inet_addr(argv[1]);
    if ( bind(sockfd, (struct sockaddr *)&sockaddr, sizeof(struct sockaddr)) == -1) 
    {
        ERR("bind");
    }

    // 启动监听
    if (listen(sockfd, 5) == -1) {
        perror("listen faild");
        return -1;
    }
    
    // 阻塞等待接收
    struct sockaddr_in ;//clientaddr;
//    socklen_t c_sock_len;
    int recvfd;
    int recv_size;
    char buf[BUF_SIZE];

    // 创建文件描述符数组
    fd_set fds, tmp_fds;
    FD_ZERO(&fds);
    FD_ZERO(&tmp_fds);
    FD_SET(sockfd, &tmp_fds);
    int max_fd = sockfd;

    printf("server ready, waitting for connection...\n");
    

    while(1) {
        fds = tmp_fds;

        if (-1 == select(max_fd + 1, &fds, NULL, NULL, NULL)) 
        {
            ERR("select");
        }

        for (int i = sockfd; i < max_fd + 1; i++) 
        {
            if (i == sockfd && FD_ISSET(i, &fds))
            {
                recvfd = accept(sockfd, NULL, NULL);

                if (recvfd == -1) {
                    perror("accept error");
                    return -1;
                } else {
                    printf("connected success.\n");
                }    

                // 注意这里更改最大文件描述符要比较一下
                max_fd = max_fd > recvfd ? max_fd : recvfd;
                FD_SET(recvfd, &tmp_fds);
            } 

            if (i != sockfd && FD_ISSET(i, &fds))
            {
                if (-1 ==(recv_size = recv(i, buf, BUF_SIZE, 0)))
                {
                    ERR("recv");
                }

                if (recv_size == 0) 
                {
                    printf("client off line");
                    FD_CLR(i, &tmp_fds);
                } else {
                    printf("[client msg]: %s\n", buf);

                    sprintf(buf, "%s received. ^_^", buf);
                    send(i, buf, BUF_SIZE, 0);

                    memset(buf, 0, BUF_SIZE);
                }
                
            }
        }

    }


    return 0;
}

